import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    View,
    Image,
    Dimensions,
    ListView,
    TouchableOpacity,
    TextInput,
    StatusBar
} from 'react-native';
import ButtonComponent from "../../common/ButtonComponent";
import Config from '../../common/Config'
import Utils from '../../common/Utils'
import {Toast,ModalIndicator} from 'teaset';
const {height, width} = Dimensions.get('window');
export default class RegistrationNextPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            verificationCode:'',

        }
    };

    register=(area,areaChange,phone)=>{
        this.bottomTextInput.blur();
        let params={
            "phone":this.props.phone,
            "checkCode":this.state.verificationCode
        };
        ModalIndicator.show("正在请求数据......");
        fetch(Config.baseApi+'/user/checkCheckCode',{
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: Utils.getParamsString(params)
        })
            .then((response) => response.json())
            .then((responseJson) => {
                ModalIndicator.hide();
                if(responseJson.status!==1){
                    Actions.push('RegistrationPasswordLoginPage',{area:area,areaChange:areaChange,phone:phone})
                }else{
                    Toast.message(responseJson.msg);
                }
            })
            .catch((error) => {
                ModalIndicator.hide();
                Toast.message(error);
            });
    };

    render() {
        return (
            <View style={styles.container}>
                <StatusBar
                    backgroundColor="white"
                    barStyle="dark-content"
                />
                <TouchableOpacity
                    onPress={()=>Actions.pop()}
                    style={styles.goBackView}>
                    <Image
                        source={ImageUtil.GoBack}
                        style={styles.goBackImg}/>
                </TouchableOpacity>
                <View style={styles.topTitle}>
                    <Text style={styles.topTitleMsg}>手机号注册</Text>
                </View>
                <View style={styles.phoneAndAreaView}>
                    <View style={styles.areaView}>
                        <Text style={styles.areaMsg}>手机号</Text>
                        <View style={styles.phoneView}>
                            <Text style={styles.phoneMsg}>+86 {this.props.phone}</Text>
                        </View>
                    </View>
                    <View style={styles.bottomTextInputView}>
                        <Text style={styles.TextInputTitleMsg}>验证码</Text>
                        <TextInput
                            ref={ (ref)=>this.bottomTextInput = ref }
                            autoCapitalize={"none"}
                            underlineColorAndroid={'transparent'}
                            defaultValue={this.state.verificationCode}
                            placeholder="请输入验证码"
                            keyboardType={"numeric"}
                            placeholderTextColor='#bababa'
                            autoCorrect={false}
                            style={styles.textInputView}
                            onChangeText={(text)=>{
                                this.setState({
                                    verificationCode:text,
                                })
                            }}/>
                        <View style={styles.verificationCodeView}>
                            <ButtonComponent borderColor={{borderColor:'#000000'}}
                                             timerCount = {10}
                                             textColor ={{color:'#000000'}}
                                             onClick = {(shouldStart) =>{
                                                 this._GetVerification(shouldStart);}}/>
                        </View>
                    </View>
                </View>
                <View style={styles.nextStepView}>
                    {this.state.verificationCode===''
                    ?(<View style={styles.nextStepButView}>
                            <Text style={styles.nextStepMsg}>下一步</Text>
                        </View>)
                    :(<TouchableOpacity
                            onPress={()=>this.register(this.props.area,this.props.areaChange,this.props.phone)}
                            style={styles.nextStepButChangeView}>
                            <Text style={styles.nextStepMsg}>下一步</Text>
                        </TouchableOpacity>)}
                </View>
            </View>
        );
    }
    _GetVerification=(shouldStart)=>{
        let login={
            "phone":this.props.phone,
        };
        ModalIndicator.show("正在请求数据......");
        fetch(Config.baseApi+'/user/getCheckCode',{
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: Utils.getParamsString(login)
        })
            .then((response) => response.json())
            .then((responseJson) => {
                ModalIndicator.hide();
                if(responseJson.status!==1){
                    shouldStart(true);
                    Toast.message('验证码已发送，注意查收')
                }else{
                    Toast.message(responseJson.msg);
                }
            })
            .catch((error) => {
                ModalIndicator.hide();
                Toast.message(error);
            });
    };
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#ffffff'
    },
    topTitle: {
        marginTop: 109,
        justifyContent: 'center',
        alignItems: 'center',
    },
    topTitleMsg: {
        fontSize: 20,
        color: '#000000'
    },
    phoneAndAreaView: {
        marginTop: 24,
        marginLeft: 30
    },
    areaView: {
        width: width - 60,
        flexDirection: 'row',
        alignItems: 'center',
        height: 40,
        borderColor: '#bababa',
        borderBottomWidth: 0.5
    },
    areaMsg: {
        fontSize: 15,
        color: '#000000'
    },
    bottomTextInputView: {
        width: width - 60,
        flexDirection: 'row',
        alignItems: 'center',
        height: 40,
        borderColor: '#bababa',
        borderBottomWidth: 1
    },
    TextInputTitleMsg: {
        fontSize: 15,
        color: '#000000',
    },
    textInputView: {
        position: 'absolute',
        left: 105,
        height: 40,
        width: width - 165,
        fontSize: 15
    },
    nextStepView: {
        marginTop:39,
        justifyContent:'center',
        alignItems:'center'
    },
    nextStepButView:{
        width:width-60,
        alignItems:'center',
        justifyContent:'center',
        backgroundColor:'#69a9dd',
        borderRadius:5,
        height:44
    },
    nextStepMsg:{
        color:'#ffffff',
        textAlign:'center',
        fontSize:17
    },
    goBackView:{
        position: 'absolute',
        left: 15,top:35
    },
    goBackMsg:{
        fontSize:16,
        color:'#3090dd',
        textAlign:'center'
    },
    phoneMsg:{
        fontSize:15,
        color:'#000000',
    },
    verificationCodeView:{
        position: 'absolute',
        right:0,
        alignItems:'center',
        justifyContent:'center',
        height:40
    },
    phoneView:{
        position: 'absolute',
        left: 105,
        height: 40,
        width: width - 165,
        justifyContent:'center',
    },
    goBackImg:{
        height:18,
        width:18
    },
    nextStepButChangeView:{
        width:width-60,
        alignItems:'center',
        justifyContent:'center',
        backgroundColor:'#3090dd',
        borderRadius:5,
        height:44
    }
});
